package com.ledim.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.View;
import com.letv.android.young.client.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 截屏/水印
 */
public class LedimScreenShotUtils {

    // 获取指定Activity的截屏
    public static String takeScreenShot(Activity activity, Bitmap bitmap) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        // 去掉标题栏
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        final Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width,
                b1.getHeight() - statusBarHeight);
        view.destroyDrawingCache();

        String path = saveImage(mergeBitmap(activity, transparentBitmap(b), bitmap), "screen.jpg");

        return path;
    }

    // 获取指定Activity的截屏
    public static String videoScreenShot(Activity activity, Bitmap bitmap, View child) {
        // View是你需要截图的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();
        // 获取状态栏高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        // 获取屏幕长和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay().getHeight();

        final Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width,
                height - statusBarHeight - child.getHeight());
        view.destroyDrawingCache();

        Bitmap videoBitmap = mergeBitmap2(activity, transparentBitmap(b), bitmap);

        Drawable db = activity.getResources().getDrawable(R.drawable.share_qrcode);
        BitmapDrawable bd = (BitmapDrawable) db;
        Bitmap bmp = bd.getBitmap();

        String path = saveImage(addBitmap(videoBitmap, bmp), "video.jpg");

        return path;
    }

    /**
     * 生成透明的bitmap
     *
     * @param firstBitmap
     * @return
     */
    public static Bitmap transparentBitmap(Bitmap firstBitmap) {
        Bitmap bitmap =
                Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(), firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);
        return bitmap;
    }

    /**
     * 合并bitmap
     *
     * @param firstBitmap
     * @param secondBitmap
     * @return
     */
    public static Bitmap mergeBitmap(Activity activity, Bitmap firstBitmap, Bitmap secondBitmap) {
        Bitmap bitmap =
                Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(), firstBitmap.getConfig());
        float f = (float) firstBitmap.getWidth() / (float) secondBitmap.getWidth();
        Canvas canvas = new Canvas(bitmap);

        Bitmap zoomBitmap = zoom(secondBitmap, f);

        canvas.drawBitmap(zoomBitmap, 0, 0, null);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);

        Drawable db = activity.getResources().getDrawable(R.drawable.play_controller_play);
        BitmapDrawable bd = (BitmapDrawable) db;
        Bitmap bmp = bd.getBitmap();

        canvas.drawBitmap(bmp,
                zoomBitmap.getWidth() / 2 - bmp.getWidth() / 2,
                zoomBitmap.getHeight() / 2 - bmp.getHeight() / 2, null);

        return bitmap;
    }

    /**
     * 合并bitmap
     *
     * @param firstBitmap
     * @param secondBitmap
     * @return
     */
    public static Bitmap mergeBitmap2(Activity activity, Bitmap firstBitmap, Bitmap secondBitmap) {
        Bitmap bitmap =
                Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(), firstBitmap.getConfig());
        float f = (float) firstBitmap.getWidth() / (float) secondBitmap.getWidth();
        Canvas canvas = new Canvas(bitmap);

        Bitmap zoomBitmap = zoom(secondBitmap, f);

        canvas.drawBitmap(zoomBitmap, 0, 0, null);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);

        //        Drawable db = activity.getResources().getDrawable(R.drawable.play_controller_play);
        //        BitmapDrawable bd = (BitmapDrawable) db;
        //        Bitmap bmp = bd.getBitmap();
        //
        //        canvas.drawBitmap(bmp, zoomBitmap.getWidth()/2 - bmp.getWidth()/2, zoomBitmap.getHeight()/2 - bmp.getHeight()/2, null);

        return bitmap;
    }

    /**
     * 合并二维码
     *
     * @param firstBitmap
     * @param secondBitmap
     * @return
     */
    public static Bitmap addBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {
        float f = (float) firstBitmap.getWidth() / (float) secondBitmap.getWidth();
        Bitmap zoomBitmap = zoom(secondBitmap, f);
        Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(),
                firstBitmap.getHeight() + zoomBitmap.getHeight(), firstBitmap.getConfig());
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(firstBitmap, new Matrix(), null);
        canvas.drawBitmap(zoomBitmap, 0, firstBitmap.getHeight(), null);
        return bitmap;
    }

    /**
     * 缩放bitmap
     *
     * @param bitmap
     * @param f
     * @return
     */
    public static Bitmap zoom(Bitmap bitmap, float f) {
        Matrix matrix = new Matrix();
        matrix.postScale(f, f); //长和宽放大缩小的比例
        Bitmap resizeBmp =
                Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }

    /**
     * bitmap保存本地
     *
     * @param bitmap
     * @return
     */
    public static String saveImage(Bitmap bitmap, String name) {
        FileOutputStream fos = null;
        String filePath = Environment.getExternalStorageDirectory() + "/letv/cache/";
        File file = new File(filePath + "img/");
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = filePath + "img/" + name;
        try {
            fos = new FileOutputStream(fileName);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 60, fos);// 把数据写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.flush();
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }

}
